package com.tyqx.hashchainbiz.service.impl;

import com.tyqx.hashchainbean.module.TrackUser;
import com.tyqx.hashchainbean.vo.TrackUserVO;
import com.tyqx.hashchainbiz.service.TokenService;
import com.tyqx.hashchainbiz.service.TrackUserService;
import com.tyqx.hashchaindao.mapper.TrackUserMapper;
import hashchainutil.common.RedisAPI;
import hashchainutil.exception.HashchainException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: dcep-spring
 * @description:
 * @author: Mr.Li
 * @create: 2020-04-19 00:43
 **/
@Service("trackUserService")
public class TrackUserServiceImpl implements TrackUserService {

    @Resource
    private TrackUserMapper trackUserMapper;

    @Resource
    private TokenService tokenService;

    @Resource
    private RedisAPI redisAPI;

//    Logger logger= LoggerFactory.getLogger(DcepUserServiceImpl.class);

    //用户验证通过
    public TrackUserVO userLandTrue(TrackUserVO trackUserVO, String userName) throws HashchainException {
        //获取dcepUser类型以保存token
        TrackUser dcepUser = trackUserMapper.getUserByName(userName);
        //生成token
        String token = tokenService.generateToken(dcepUser);
        //移除旧token
        tokenService.removeToken(token);
        //baocuntoken
        tokenService.saveToken(token, dcepUser);
/*
        logger.debug("id：" + dcepUser.getUserId() + ";昵称：" + dcepUser.getUserName() + "的token为：" + token);
*/
        //获取时间及规定格式
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(date);

        //修改登录时间
        try {
            trackUserMapper.updateLoginTime(trackUserVO.getId().toString(),sdf.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //保存redis
        redisAPI.set("userLand:" + dcepUser.getUserName(), time);
//        logger.debug("id：" + dcepUser.getUserId() + ";账号为：" + dcepUser.getUserCode() + "的redis为：" + time);
        //清除返回数据的密码
        trackUserVO.setPassword("");
        trackUserVO.setToken(token);
        return trackUserVO;
    }

    @Override
    public TrackUserVO login(String userName, String userPassword) throws HashchainException {
        //通过账号和登陆标识获取用户
        TrackUserVO trackUserVO=trackUserMapper.userLand(userName);
        if (trackUserVO==null){
            throw new HashchainException("用户不存在", "100002");
        }
        //加密用户输入的密码
        //String password = DigestUtil.hmacSign(userPassword);
        PasswordEncoder passwordEncoder=new BCryptPasswordEncoder();

        //String encodedPassword=passwordEncoder.encode(userPassword);
        //和数据库查出的用户加密密码对比
        if (passwordEncoder.matches(userPassword,trackUserVO.getPassword())) {

            return userLandTrue(trackUserVO, userName);
        } else {
            throw new HashchainException("密码错误", "100001");
        }
    }

    @Override
    public TrackUser getUserByCode(String userName) throws HashchainException{
        return trackUserMapper.getUserByName(userName);
    }


}
